<template>
  <div>
    <el-dialog
      title="监测公司绑定→绑定监测公司"
      :visible.sync="show"
      :before-close="cancel"
      width="40%"
    >
      <div class="formBox">
        <div class="formHead">绑定监测公司</div>
        <div class="formWhite">
          <div class="left"><span class="red">*</span>监测公司</div>
          <div class="right">
            <el-select
              v-model="submitForm.monitorId"
              size="small"
              placeholder="请选择"
              @change="changeItem"
            >
              <el-option
                v-for="item in jcCompanyList"
                :key="item.monitorId"
                :label="item.monitorName"
                :value="item.monitorId"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="formGray">
          <div class="left"><span class="red">*</span>账号</div>
          <div class="right">
            <el-input
              v-model="submitForm.accountNumber"
              placeholder="请输入"
              size="small"
            ></el-input>
          </div>
        </div>
        <div class="formWhite">
          <div class="left"><span class="red">*</span>密码</div>
          <div class="right">
            <el-input
              v-model="submitForm.accountPassword"
              placeholder="请输入"
              size="small"
            ></el-input>
          </div>
        </div>
        <div class="formGray">
          <div class="left"><span class="red">*</span>所属白蚁防治公司</div>
          <div class="right">
            <el-select
              v-model="submitForm.controlId"
              size="small"
              placeholder="请选择"
              filterable
              disabled
              @change="changeItem2"
            >
              <el-option
                v-for="item in companyList"
                :key="item.controlId"
                :value="item.controlId"
                :label="item.controlName"
              ></el-option>
            </el-select>
            <!-- <el-input
              v-model="submitForm.controlName"
              placeholder="请输入"
              size="small"
              disabled
            ></el-input> -->
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="enSure" size="small">确定</el-button>
        <el-button type="primary" plain @click="cancel" size="small"
          >取消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import {
  insertMonitorCompany, // 绑定监测公司数据-新增
  queryMonitorCompanyData, // 监测公司查询
} from "@/api/manage/asset";
import { getControlCompanyData } from "@/api/manage/approval";
export default {
  name: "addMsg",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      submitForm: {
        monitorId: "",
        monitorName: "",
        accountNumber: "",
        accountPassword: "",
        controlName: "",
        controlId: "",
      },
      companyList: [],
      jcCompanyList: [],
    };
  },
  computed: {
    ...mapGetters(["parentMsg"]),
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // this.submitForm.controlName = this.parentMsg.orgName;
      queryMonitorCompanyData().then((res) => {
        if (res.errorCode == "000000") {
          this.jcCompanyList = res.data;
        }
      });
      getControlCompanyData().then((res) => {
        if (res.errorCode === "000000") {
          this.companyList = res.data;
          this.submitForm.controlId = res.data[0].controlId;
          this.submitForm.controlName = res.data[0].controlName;
        }
      });
    },
    enSure() {
      console.log("提交111", this.submitForm);
      if (!this.submitForm.monitorId) {
        this.$modal.msgWarning("请选择监测公司！");
      } else if (!this.submitForm.accountNumber) {
        this.$modal.msgWarning("请填写账号！");
      } else if (!this.submitForm.accountPassword) {
        this.$modal.msgWarning("请填写密码！");
      } else if (!this.submitForm.controlId) {
        this.$modal.msgWarning("请选择所属白蚁防治公司！");
      } else {
        insertMonitorCompany(this.submitForm).then((res) => {
          if (res.errorCode == "000000" && res.data) {
            this.$modal.msgSuccess("绑定成功！");
            this.$emit("closeMsg", true);
            this.getList();
            this.submitForm = {
              monitorId: "",
              monitorName: "",
              accountNumber: "",
              accountPassword: "",
              controlName: "",
              controlId: "",
            };
          } else {
             this.$modal.msgWarning(res.errorMsg);
          }
        });
      }
    },
    cancel() {
      this.$emit("closeMsg");
    },
    changeItem(val) {
      console.log(val, "0000");
      this.submitForm.monitorName = this.jcCompanyList.find(
        (item) => item.monitorId === val,
      ).monitorName;
    },
    changeItem2(val) {
      this.submitForm.controlName = this.companyList.find(
        (item) => item.controlId === val,
      ).controlName;
    },
  },
};
</script>
<style lang="scss" scoped>
.headerTable {
  background: #5d6d7f;
  height: 48px;
  line-height: 48px;
  text-align: center;
  width: 100%;
  color: #fff;
  font-weight: Bold;
  font-size: 14px;
  font-family:
    Helvetica Neue,
    Helvetica,
    PingFang SC,
    Hiragino Sans GB,
    Microsoft YaHei,
    Arial,
    sans-serif;
  border: 1px solid #e3e3e3;
  border-bottom: 0;
}

.formBox {
  .left {
    display: inline-block;
    height: 44px;
    line-height: 44px;
    width: 140px;
    padding-right: 15px;
    border-left: 1px solid #e4e4dc;
    border-right: 1px solid #e4e4dc;
    font-family: MicrosoftYaHei;
    font-size: 12px;
    color: #303133;
    text-align: right;
    vertical-align: top;
  }
  .right {
    display: inline-block;
    width: calc(100% - 140px);
    height: 44px;
    border-right: 1px solid #e4e4dc;
    padding: 6px 15px;
    .el-select {
      width: 100% !important;
    }
    .el-input {
      width: 100% !important;
    }
  }
  .formHead {
    width: 100%;
    height: 44px;
    background-color: #73879c;
    height: 44px;
    line-height: 44px;
    text-align: center;
    font-family: MicrosoftYaHei, MicrosoftYaHei;
    font-weight: bold;
    font-size: 12px;
    color: #ffffff;
  }
  .formWhite {
    width: 100%;
    height: 44px;
    background-color: #fff;
    border-bottom: 1px solid #e4e4dc;
  }
  .area {
    .areaLeft {
      width: 40% !important;
      margin-right: 10px;
    }
    .areaRight {
      width: calc(60% - 10px) !important;
    }
  }
  .formGray {
    width: 100%;
    height: 44px;
    background-color: #eef0f2;
    border-bottom: 1px solid #e4e4dc;
  }
}
.red {
  color: red;
  margin-right: 2px;
  vertical-align: top;
}
::v-deep .el-dialog__header {
  border-bottom: 1px solid #e9e9ec;
}
::v-deep .el-dialog__footer {
  border-top: 1px solid #e9e9ec;
}
</style>
